package io.github.talelin.latticy.controller.v1;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.stock.StockAdjustDTO;
import io.github.talelin.latticy.dto.stock.StockDTO;
import io.github.talelin.latticy.dto.stock.StockPageDTO;
import io.github.talelin.latticy.dto.stock.StockVO;
import io.github.talelin.latticy.model.StockDO;
import io.github.talelin.latticy.service.StockService;
import io.github.talelin.latticy.util.PageHelpUtil;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 库存前端控制器
 *
 * @author generator@wulongbo
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/v1/stock")
@PermissionModule(value = "库存")
public class StockController {


    @Resource
    private StockService stockService;


    @GetMapping("/page")
    @PermissionMeta(value = "库存查询")
    @GroupRequired
    public PageResponseVO<StockVO> page(StockPageDTO stockPageDTO) {
        return PageUtil.build(stockService.getStockList(stockPageDTO));
    }

    /**
     * 入库单审核通过保存待入库库存
     *
     * @param stockDTO
     * @return
     */
    @PostMapping("/insertOrUpStock")
    @PermissionMeta(value = "入库单审核通过保存待入库库存")
    public CreatedVO insertOrUpStock(@Validated @RequestBody StockDTO stockDTO) {
        Assert.isTrue(stockService.insertOrUpStock(stockDTO), "18515");
        return new CreatedVO();
    }


    /**
     * 调整库存
     * @param stockDTO
     * @return
     */
    @GetMapping("/adjustStock")
    @PermissionMeta(value = "调整库存")
    @GroupRequired
    public UpdatedVO adjustStock(@Validated @RequestBody List<StockAdjustDTO> stockDTO){
        Assert.isTrue(stockService.adjustStock(stockDTO), "18515");
        return new UpdatedVO();
    }
}
